# Contributor: rubicon <rubicon@mailo.com>
# Maintainer: rubicon <rubicon@mailo.com>
pkgname=ccl
pkgver=1.12.1
pkgrel=0
pkgdesc="Clozure Lisp compiler"
url="https://github.com/Clozure/ccl"
arch="x86_64"
license="Apache-2.0"
makedepends="linux-headers m4"
subpackages="$pkgname-dev $pkgname-doc"
_testsgitrev=0478abddb34dbc16487a1975560d8d073a988060
source="$pkgname-$pkgver.tar.gz::https://github.com/Clozure/ccl/releases/download/v$pkgver/ccl-$pkgver-linuxx86.tar.gz
	https://github.com/Clozure/ccl-tests/archive/$_testsgitrev/ccl-$pkgver-tests.tar.gz
	ccl.sh
	musl-fixes.patch
	makefile64.patch
	"
builddir="$srcdir/$pkgname"
_testsdir="$srcdir/$pkgname-tests-$_testsgitrev"

build() {
	msg "Rebuilding lisp kernel"
	make -j1 -C lisp-kernel/linuxx8664 clean all

	msg "Rebuilding heap image"
	./lx86cl64 --no-init --quiet --batch \
		-e '(ccl:rebuild-ccl :clean t)' \
		-e '(ccl:quit)'
}

check() {
	cd "$_testsdir"
	make clean

	# Skip ANSI tests, as there are 21679 of them and
	# we just want to know if CCL compiled successfully.
	# Besides that, 4 tests fail: CL-TEST::ACOS.6, ACOS.8,
	# CL-TEST::COMPILE-FILE.2, and COMPILE-FILE.2A.
	"$builddir"/lx86cl64 --no-init --batch \
		--load "load.lisp" \
		-e '(run-tests :ansi nil :exit t :verbose t)'
}

package() {
	mkdir -vp "$pkgdir"/usr/bin \
		"$pkgdir"/usr/lib/ccl \
		"$pkgdir"/usr/share/ccl \
		"$pkgdir"/usr/share/doc

	install -Dv "$srcdir"/ccl.sh "$pkgdir"/usr/bin/ccl
	cp -av	compiler level-* lib* lisp-kernel objc-bridge \
		tools xdump x86-headers64 lx86cl64* \
		"$pkgdir"/usr/lib/ccl/
	cp -av	examples scripts "$pkgdir"/usr/share/ccl/
	cp -av	doc "$pkgdir"/usr/share/doc/ccl
	cp -av	README.md LICENSE "$pkgdir"/usr/share/doc/ccl/
}

dev() {
	default_dev

	# Don't move examples to the -dev subpackage
	local file; for file in $(find "$subpkgdir"/usr/share -type f); do
		file=${file#$subpkgdir/}
		mv -v "$subpkgdir"/"$file" "$pkgdir"/"$file"
		rmdir	-vp "$subpkgdir"/"${file%/*}" \
			--ignore-fail-on-non-empty
	done

	local file; for file in $(find "$pkgdir"/usr/lib/ccl \
		-type f \( \
		-name '*.o' -o \
		-name '*.*fsl' \))
	do
		file=${file#$pkgdir/}
		amove "$file"
	done

}

sha512sums="
7a4ab6bbb11e1ed3a13512f03626d21b6e97ea8e1f4e34647272fab24ea37c8e54b5471f9f20ad1fffacadb9825ec3b79e72fa9d27c426e01eac7bd3601e62c8  ccl-1.12.1.tar.gz
eb6f8c22a811841747de4aab5645dae2a19bc077a19ade39324d1cf29beb2eb16c3a1e5c7ef8cea2da750da95795ccd70bb3f74fd4b35461d435f10f2f58a499  ccl-1.12.1-tests.tar.gz
4e441a81d4ae6365fc61b3287492d743c7757529d921508990f5251fbe22d876cf478321b1c2ac22eaefedd1954070f9b72e3d530b15d53c4cebc633ab9a411f  ccl.sh
6ec343066aa4d1699925630795808e1b8eb4fad0fcca499db2bc5da31cb5aff8893fa63ee9f0d0807fe46b5321119485efbff0f4bef515a9149f134ff31a6617  musl-fixes.patch
ee376f5102ccf07ed579503b701ac9b2940c9872b61abe0a95aa05083cb4a37ed2c85c11d040883ea890ff3b807199bf02a1ad0e6d57a2b6bd7a0f99d8a6f56a  makefile64.patch
"
